#parse argument

architecture=$1
matrix_decompressed_file=$2
matrix_comp_file=$3
output_dir=$4
lorem_ipsum_file=$5

permutation_str=""

# calculate size of each block

lorem_ipsum_file_size=$(du -b $lorem_ipsum_file | cut -f1)
length_block=$(expr $lorem_ipsum_file_size / 4 )
offset0=0
offset1=$(expr $offset0 + $length_block )
offset2=$(expr $offset1 + $length_block )
offset3=$(expr $offset2 + $length_block )
length3=$(expr $lorem_ipsum_file_size - $offset3 )
tag_blocks=25

# generate blocks

str="
xrnlib-cli \
    --make-block -offset $offset0 -length $length_block -tag $tag_blocks -plain-text $lorem_ipsum_file -block-file $output_dir/block_file0.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --make-block -offset $offset1 -length $length_block -tag $tag_blocks -plain-text $lorem_ipsum_file -block-file $output_dir/block_file1.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --make-block -offset $offset2 -length $length_block -tag $tag_blocks -plain-text $lorem_ipsum_file -block-file $output_dir/block_file2.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --make-block -offset $offset3 -length $length3 -tag $tag_blocks -plain-text $lorem_ipsum_file -block-file $output_dir/block_file3.xf
"
echo ""; echo $str; echo ""
eval $str

# merge block 1 and 3 and expect error because not contiguous

str="
xrnlib-cli \
    --logging-conf -lib-error-log $output_dir/wrong_merge.txt \
    --combine-blocks -block-file $output_dir/block_file1.xf -block-file $output_dir/block_file3.xf -combined-block-file $output_dir/block_file13.xf
"
echo ""; echo $str; echo ""
eval $str

str=" 
if ! [ -s $output_dir/wrong_merge.txt ]; then echo FAIL ; exit ; fi ; \
if [ $( wc -l $output_dir/wrong_merge.txt | awk '{print $1}' ) != 1 ] ; then echo FAIL ; exit ; fi
"
echo ""; echo $str; echo ""
eval $str

# merge block 0 and 1 and see if the blocks are complete (they are not without 2 and 3)

str="
xrnlib-cli \
    --combine-blocks -block-file $output_dir/block_file0.xf -block-file $output_dir/block_file1.xf -combined-block-file $output_dir/block_file01.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --logging-conf -lib-info-log $output_dir/lib_info_block01.json \
    --check-complete-block -block-file $output_dir/block_file01.xf
"
echo ""; echo $str; echo ""
eval $str

str=" 
if ! [ -s $output_dir/lib_info_block01.json ]; then echo FAIL ; exit ; fi ; \
if grep -q PASS \"$output_dir/lib_info_block01.json\" ; then echo FAIL ; exit ; fi
"
echo ""; echo $str; echo ""
eval $str

# merge all the other blocks and check if they are complete

str="
xrnlib-cli \
    --combine-blocks -block-file $output_dir/block_file01.xf -block-file $output_dir/block_file2.xf -combined-block-file $output_dir/block_file012.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --combine-blocks -block-file $output_dir/block_file012.xf -block-file $output_dir/block_file3.xf -combined-block-file $output_dir/block_file0123.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --logging-conf -lib-info-log $output_dir/lib_info_block0123.json \
    --check-complete-block -block-file $output_dir/block_file0123.xf
"
echo ""; echo $str; echo ""
eval $str

str=" 
if ! [ -s $output_dir/lib_info_block0123.json ]; then echo FAIL ; exit ; fi ; \
if grep -q FAIL \"$output_dir/lib_info_block0123.json\" ; then echo FAIL ; exit ; fi
"
echo ""; echo $str; echo ""
eval $str

# strip header and check against the original file

str="
xrnlib-cli \
    --strip-header-from-block -block-file $output_dir/block_file0123.xf -plain-text $output_dir/stripped_lorem_ipsum.txt
"
echo ""; echo $str; echo ""
eval $str

str=" 
cmp --silent $output_dir/stripped_lorem_ipsum $lorem_ipsum_file && echo 'FAIL'
"
echo ""; echo $str; echo ""
eval $str

# combine everything in one block in order

str="
xrnlib-cli \
    --combine-blocks -block-file $output_dir/block_file0.xf -block-file $output_dir/block_file1.xf -block-file $output_dir/block_file2.xf -block-file $output_dir/block_file3.xf -combined-block-file $output_dir/block_file0123_2.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --logging-conf -lib-info-log $output_dir/lib_info_block0123_2.json \
    --check-complete-block -block-file $output_dir/block_file0123_2.xf
"
echo ""; echo $str; echo ""
eval $str

str=" 
if ! [ -s $output_dir/lib_info_block0123_2.json ]; then echo FAIL ; exit ; fi ; \
if grep -q FAIL \"$output_dir/lib_info_block0123_2.json\" ; then echo FAIL ; exit ; fi
"
echo ""; echo $str; echo ""
eval $str

# combine everything in one instruction out of order

str="
xrnlib-cli \
    --combine-blocks -block-file $output_dir/block_file1.xf -block-file $output_dir/block_file0.xf -block-file $output_dir/block_file3.xf -block-file $output_dir/block_file2.xf -combined-block-file $output_dir/block_file0123_3.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --logging-conf -lib-info-log $output_dir/lib_info_block0123_3.json \
    --check-complete-block -block-file $output_dir/block_file0123_3.xf
"
echo ""; echo $str; echo ""
eval $str

str=" 
if ! [ -s $output_dir/lib_info_block0123_3.json ]; then echo FAIL ; exit ; fi ; \
if grep -q FAIL \"$output_dir/lib_info_block0123_3.json\" ; then echo FAIL ; exit ; fi
"
echo ""; echo $str; echo ""
eval $str

# combine everything in one instruction out of order and overlaps

str="
xrnlib-cli \
    --make-block -offset $offset1 -length $length_block -tag $tag_blocks -plain-text $lorem_ipsum_file -block-file $output_dir/block_file4.xf
"
echo ""; echo $str; echo ""
eval $str


str="
xrnlib-cli \
    --combine-blocks -block-file $output_dir/block_file1.xf -block-file $output_dir/block_file0.xf -block-file $output_dir/block_file4.xf -block-file $output_dir/block_file3.xf -block-file $output_dir/block_file2.xf -combined-block-file $output_dir/block_file0123_4.xf
"
echo ""; echo $str; echo ""
eval $str

str="
xrnlib-cli \
    --logging-conf -lib-info-log $output_dir/lib_info_block0123_4.json \
    --check-complete-block -block-file $output_dir/block_file0123_4.xf
"
echo ""; echo $str; echo ""
eval $str

str=" 
if ! [ -s $output_dir/lib_info_block0123_4.json ]; then echo FAIL ; exit ; fi ; \
if grep -q FAIL \"$output_dir/lib_info_block0123_4.json\" ; then echo FAIL ; exit ; fi
"
echo ""; echo $str; echo ""
eval $str
